package org.goyo.main.web.controller.pur;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.goyo.common.baseBean.AjaxResult;
import org.goyo.common.utils.ToolUtils;
import org.goyo.main.domain.entity.pur.Payment;
import org.goyo.main.services.pur.IPaymentService;
import org.goyo.main.web.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * 采购订单
 * 
 * @author chenchuan
 * 2014年11月12日
 */
@Controller
@RequestMapping("/payment")
public class PaymentController extends BaseController  {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Resource
	private IPaymentService paymentService;
	
	/**
	 * 保存或者更新
	 * 
	 * @author chenchuan
	 * 2014年11月9日
	 */
	@RequestMapping("/saveOrUpdatePayment")
	@ResponseBody
	public AjaxResult saveOrUpdatePayment(HttpServletRequest request){
		
		AjaxResult result = null;
		try{
			String gridData = request.getParameter("gridData");
			String delData = request.getParameter("delData");
			Object[] delUuids = null;
			List<Payment> gridList = null;
			if(delData != null &&!"null".equals(delData) &&!"undefined".equals(delData)){
				JSONArray delArray = JSONArray.fromObject(delData);
				List<Payment> delEntry = JSONArray.toList(delArray, Payment.class);
				delUuids = ToolUtils.getFiles(delEntry,"uuid").toArray();
			}
			if(gridData != null &&!"null".equals(gridData) &&!"undefined".equals(gridData)){
				JSONArray gridArray = JSONArray.fromObject(gridData);
				gridList = JSONArray.toList(gridArray, Payment.class);
			}
			BigDecimal amount = paymentService.saveOrUpdateInfo(gridList, delUuids);
			result = result.resultSuccess("保存成功", amount);
		}catch(Exception e){
		    result = AjaxResult.resultFail("保存失败", null);
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * 修改
	 * 
	 * @author chenchuan
	 * 2014年11月9日
	 */
	@RequestMapping("/alterPayment")
	public String alterPayment(HttpServletRequest request,Model model){
		String parentId = request.getParameter("parentId");
		model.addAttribute("parentId", parentId);
		return "/pur/PaymentEdit";
	}
	
	
	/**
	 * list列表数据加载
	 * 
	 * @author chenchuan
	 * 2014年11月9日
	 */
	@RequestMapping("/listData")
	@ResponseBody
	public String ListData(HttpServletRequest request){
		JSONObject data = null;
		String parentId = request.getParameter("parentId");
		List<Payment> list = null;
		try {
			if(parentId != null  && !"".equals(parentId)){
				list = paymentService.findByParentId(parentId);
			}
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("Rows", list);
			data = JSONObject.fromObject(map);

		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return data.toString();
		
	}
	

}
